﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GoTextPalm.Gui
{
    public partial class Animator : UserControl
    {
        private Graphics graphics;

        public Animator()
        {
            InitializeComponent();
            graphics = CreateGraphics();
        }

        public Boolean Started
        {
            get
            {
                return timer.Enabled;
            }
            set
            {
                if (!value)
                {
                    index = 0;
                    graphics.Clear(BackColor);
                }
                timer.Enabled = value;
            }
        }

        private int index = 0;

        private void DrawFrame(int iFrame)
        {
            try
            {
                Image bitmap = imageList.Images[iFrame];
                Rectangle r = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                graphics.DrawImage(bitmap, 0, 0, r, GraphicsUnit.Pixel);
            }
            catch (ObjectDisposedException ex)
            {
                timer.Dispose();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (index == imageList.Images.Count)
                index = 0;
            DrawFrame(index++);
        }
    }
}
